// miniprogram/pages/manageShop/manageShop.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coverPicture: "",
    shopName: "",
    shopIntro: "",
    shopAddress: "",
    min: 6,
    max: 140,
    currentWordNumber:0,
    texts:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  input_shopName(e) {
    this.setData({
      shopName: e.detail.value
    });
  },
  input_shopIntro(e) {
    this.setData({
      shopIntro: e.detail.value
    });
  },
  input_shopAddress(e) {
    this.setData({
      shopAddress: e.detail.value
    });
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);

    //最少字数限制
    if (len <= this.data.min)
      this.setData({
        texts: "加油，够5个字可以得20积分哦"
      });
    else if (len > this.data.min)
      this.setData({
        texts: " "
      });

    //最多字数限制
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
    this.setData({
      currentWordNumber: len //当前字数
    });
  },
  doUpload(){
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album", "camera"],
      success: function(res) {
        wx.showLoading({
          title: "上传中"
        });

        const filePath = res.tempFilePaths[0];

        // 上传图片
        const cloudPath = "my-image" + filePath.match(/\.[^.]+?$/)[0];
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log("[上传文件] 成功：", res);

            app.globalData.fileID = res.fileID;
            app.globalData.cloudPath = cloudPath;
            app.globalData.imagePath = filePath;

          },
          fail: e => {
            console.error("[上传文件] 失败：", e);
            wx.showToast({
              icon: "none",
              title: "上传失败"
            });
          },
          complete: () => {
            wx.hideLoading();
          }
        });
      },
      fail: e => {
        console.error(e);
      }
    });
  }
});
